import os
import re


def loadFile(fname):
    with open(fname, 'r') as f:
        contents = f.readlines()
    return contents

def outFile(fname, contents):
    with open(fname, 'w') as f:
        f.writelines(contents)
    return

def doReplace(content, findString, subOut, subIn):
    for index, line in enumerate(content):
        if re.match(findString, line):
            content[index] = re.sub(subOut, subIn, line)
            return content, index

def insertData(content, index):
    # -- ENTER NETWORK SETTINGS AND REMOVE COMMENTS
    #address = '\taddress xxx.xxx.xxx.xxx\n'
    #netmask = '\tnetmask xxx.xxx.xxx.xxx\n'
    #gateway = '\tgateway xxx.xxx.xxx.xxx\n'
    #dns = '\tdns-nameservers xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx\n'
    index += 1
    content.insert(index, dns)
    content.insert(index, gateway)
    content.insert(index, netmask)
    content.insert(index, address)
    return content

#INFILE = 
#OUTFILE =

pattern = re.compile(r'^iface [a-zA-Z0-9]{1,5} inet dhcp$', re.I)

fname = loadFile(INFILE)
subbed, index = doReplace(fname, pattern, 'dhcp', 'static\n')
final = insertData(subbed, index)
outFile(OUTFILE, final)

            
